/*
This file generates Tables 1 and Appendix 1 and Appendix 2 using the balanced-data.dta generated in the 2 - EUSILC - Analysis - part1.Rmd, from the dataset stemming from the EU-SILC.

Please change this path into your personal folder where the data files were placed according to the readme file explanations.
*/

	global read "folder/data/"
	cd "$read"
****************************************************************************************
************************Part1 - Table 1   **********************************************
****************************************************************************************
if 1>0 {
*
use "balanced-data.dta", clear
keep if rb020=="UK"
xtset i period
tab health
replace health=6-health
bys i: gen fhealth=F1.health
tab health fhealth, row

}

****************************************************************************************
************************Appendix - Tables 1 and 2 **************************************
****************************************************************************************

if 1>0 {

use "balanced-data.dta", clear

*Table A1
replace health = 4 if health==5
replace health=6-health

bys health: sum ltdhi
xtset i year
bys i: gen dhealth=D1.health
bys i: gen dltdhi=D1.ltdhi
table dhealth, c(mean dltdhi)
bys dhealth: sum dltdhi


*Table A2

tab year
tab health

sum
tab emp
tab ageg
tab urbanisation
tab educ6
tab period
}

****************************************************************************************
************************Simulation - Tables  ******************************************
****************************************************************************************

if 1>0 {

use "balanced-data.dta", clear

	replace period=period-1
	tab period
	replace health = 4 if health==5
	replace health=5-health

	ren i iold
	egen i=group(iold)
	
	keep health child married emp ltdhi i period

	
	g unemp=(emp==2) 
	g retired=(emp==3) 
	g other=(emp==4)
	
	drop emp
	reshape wide health child married unemp retired other ltdhi, i(i) j(period)

	keep i health0 ltdhi1 ltdhi2 ltdhi3 child1 child2 child3 married1 married2 married3 unemp1 retired1 other1 unemp2 retired2 other2 unemp3 retired3 other3
	order i health0 ltdhi1 ltdhi2 ltdhi3 child1 child2 child3 married1 married2 married3 unemp1 unemp2 unemp3 retired1 retired2 retired3 other1 other2 other3
	
		export delimited using "sample.csv", replace
}

exit
